home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / letters / moser.c < prev   
Encoding:
C/C++ Source or Header  |  1993-10-26  |  2.1 KB  |  76 lines

  1. @CSOURCE7 = /* File: LOG.H */<R>
  2. #define DEF_FUNC_NAME(s)      static char _FUNCTION_NAME[] = s;<R>
  3. #define FUNC_NAME()           (_FUNCTION_NAME)<R>
  4. <R>
  5. #define DEF_MODULE_NAME(s)    static char _MODULE_NAME[] = s;<R>
  6. #define MODULE_NAME()         (_MODULE_NAME)<R>
  7. <R>
  8. extern int writelog(char *format, ...);<R>
  9. /* [Other function prototypes go here, but they're not important] 
  10. */<R>
  11. /* EOF (LOG.H) */<R>
  12. <R>
  13. All other modules #include the LOG.H header file.  Here's how the 
  14. macros<R>
  15. and functions defined in LOG.H are used:<R>
  16. <R>
  17. /* File: STRUTILS.C */<R>
  18. #include <<<<stdio.h>>>><R>
  19. #include <<<<string.h>>>><R>
  20. #include "log.h"<R>
  21. <R>
  22. DEF_MODULE_NAME(__FILE__)<R>
  23. <R>
  24. char *double2comma(double f, int places)<R>
  25. {<R>
  26.     DEF_FUNC_NAME("double2comma")<R>
  27.     static char tmp[40];  /* Return value */<R>
  28.     char *s;<R>
  29.     int i=0;<R>
  30. <R>
  31.     if ((places <<<< 0) || (places >>>> 38)) {<R>
  32. #ifndef NDEBUG<R>
  33.          writelog("%s.%s(): param 'places' (%d) is invalid.\n",<R>
  34.               MODULE_NAME(), FUNC_NAME(),<R>
  35.               places<R>
  36.          );<R>
  37. #endif<R>
  38.          return (tmp);<R>
  39.     }<R>
  40. <R>
  41.     sprintf(tmp, "%#.*f", places, f);<R>
  42. <R>
  43.     if ((s = strchr(tmp, '.')) == NULL) {<R>
  44. #ifndef NDEBUG<R>
  45.          writelog("%s.%s(): strchr() returned NULL when seeking '.'\<R>
  46. in '%s'.\n",<R>
  47.               MODULE_NAME(), FUNC_NAME(),<R>
  48.               tmp<R>
  49.          );<R>
  50. #endif<R>
  51.     } else {<R>
  52.          if (!places) {<R>
  53.               /* Get rid of trailing '.': */<R>
  54.               *s = '\0';<R>
  55.          }<R>
  56. <R>
  57.          while (--s >>>> tmp) {<R>
  58.               if (i++ == 2) {<R>
  59.                    /* Are we just to the right of '-'? */<R>
  60.                    if (s[-1] == '-') break;
  61.  
  62. @CSOURCE7 =                    /* Insert a comma: */<R>
  63.                    i = 0;<R>
  64.                    /* Shift segment right to make room for ',': */<R>
  65.                    memmove(s+1, s, strlen(s)+1);<R>
  66.                    *s = ',';<R>
  67.               }<R>
  68.          }<R>
  69.     }<R>
  70.     return (tmp);<R>
  71. }<R>
  72. <R>
  73. /* [More functions for this module go here] */<R>
  74. /* EOF */
  75.  
  76.